package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.BannerMapper;
import com.gxa.pingan.pojo.Banner;
import com.gxa.pingan.service.BannerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @Author ChenJin
 * @date 2021/10/25 19:24
 * Description:
 **/
@Service("bannerService")
@Transactional(rollbackFor = {Throwable.class})
public class BannerServiceImpl implements BannerService {

    @Resource
    private BannerMapper bannerMapper = null;

    /**
     * 根据id修改轮播图是否可见
     *
     * @param id 轮播图编号
     * @return
     */
    @Override
    public void updateType(Long id,Boolean type) throws BusinessException {
        Banner banner = bannerMapper.selectBanner(id);
        if (banner == null){
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        banner.setInUsed(type);
        bannerMapper.update(banner);
    }

    /**
     * 添加轮播图
     *
     * @param banner 轮播图对象
     */
    @Override
    public void insert(Banner banner) {
        List<Banner> list = bannerMapper.getList();
        int i = 0;
        // 找到最大的序号，新增的时候，新增的轮播图的排序序号为最大序号+1
        for (Banner getBanner: list) {
            Integer rank = getBanner.getRank();
            if (rank > i){
                i = rank;
            }
        }
        // 点击量新增默认为0
        banner.setCount(0);
        banner.setRank(i + 1);
        // 新增默认为可见
        banner.setInUsed(true);
        // 新增后端默认设置创建时间
        banner.setRegTime(new Timestamp(System.currentTimeMillis()));
        bannerMapper.addBanner(banner);
    }

    /**
     * 根据id删除轮播图
     *
     * @param id 轮播图编号
     */
    @Override
    public void delete(Long id) throws BusinessException {
        // 先根据id查询是否存在
        Banner banner = bannerMapper.selectBanner(id);
        if (banner==null){
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        // 先将需要删除的轮播图的排序序号存起来，用于改变其他序号
        Integer rank = banner.getRank();
        // 删除轮播图操作
        bannerMapper.deleteBanner(id);
        // 删除的时候，将其他轮播图的序号改变
        bannerMapper.move(rank);

    }

    /**
     * 修改轮播图信息
     *
     * @param banner 轮播图对象
     * @throws BusinessException
     */
    @Override
    public void update(Banner banner) throws BusinessException {
        Banner getBanner = bannerMapper.selectBanner(banner.getId());
        if (getBanner==null){
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        banner.setRank(getBanner.getRank());
        banner.setInUsed(getBanner.getInUsed());
        banner.setRegTime(getBanner.getRegTime());
        banner.setCount(getBanner.getCount());
        bannerMapper.update(banner);
    }

    /**
     * 移动轮播图
     *
     * @param id   轮播图对象
     * @param type 0：上移 1：下移
     */
    @Override
    public void move(Long id, Integer type) throws BusinessException {
        // 先根据id查询该轮播图是否存在
        Banner banner = bannerMapper.selectBanner(id);
        Integer rank = banner.getRank();
        int i = 0;
        if (banner==null){
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        // 如果0则为上移
        if (type == 0){
            // 先找到该轮播图上面的轮播图
            Banner selectByRank = bannerMapper.selectByRank(rank + 1);
            // 将该轮播图的序号改成0，上面的轮播图排序序号改成该轮播图排序序号
            banner.setRank(i);
            selectByRank.setRank(rank);
            bannerMapper.update(banner);
            bannerMapper.update(selectByRank);

            // 然后再将该轮播图的排序序号改成上面的轮播图序号
            banner.setRank(rank+1);
            bannerMapper.update(banner);

            // 如果1则为下移
        }else if (type == 1){
            // 先找到该轮播图下面的轮播图
            Banner selectByRank = bannerMapper.selectByRank(rank - 1);
            // 将该轮播图的序号改成0，上面的轮播图排序序号改成该轮播图排序序号
            banner.setRank(i);
            selectByRank.setRank(rank);
            bannerMapper.update(banner);
            bannerMapper.update(selectByRank);

            // 然后再将该轮播图的排序序号改成上面的轮播图序号
            banner.setRank(rank-1);
            bannerMapper.update(banner);
        }else {
            throw new BusinessException(ExceptionCode.FAILED_OPERATION);
        }
    }

    /**
     * 获取轮播图列表
     * @param page 页码
     * @param size 每页大小
     * @return
     * @throws BusinessException
     */
    @Override
    public List<Banner> getListByPage(Integer page, Integer size) throws BusinessException {
        List<Banner> listByPage = bannerMapper.getListByPage((page-1)*size, size);
        if (listByPage.size()==0){
            throw new BusinessException(ExceptionCode.NO_SUCH);
        }
        return listByPage;
    }

    /**
     * 获取全查询轮播图的数量
     *
     * @return 返回轮播图的总数量
     * @throws BusinessException
     */
    @Override
    public Integer getListSize() throws BusinessException {
        List<Banner> list = bannerMapper.getList();
        if (list.size()==0){
            throw new BusinessException(ExceptionCode.NO_SUCH);
        }
        return list.size();
    }


}
